﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class AdminLog
    {
        private readonly KeRui.DAL.AdminLog dal = new KeRui.DAL.AdminLog();

        public static void Add(int UserID, string Flag, string Log)
        {
            KeRui.BLL.AdminLog log = new KeRui.BLL.AdminLog();
            KeRui.Model.AdminLog model = new KeRui.Model.AdminLog();
            model.UserID = UserID;
            model.Flag = Flag;
            model.Log = Log;
            model.CreateIP = Utils.GetRealIP();
            log.AddModel(model);
        }

        public int AddModel(KeRui.Model.AdminLog model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.AdminLog> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.AdminLog> list = new List<KeRui.Model.AdminLog>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.AdminLog item = new KeRui.Model.AdminLog();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    item.Username = dt.Rows[i]["Username"].ToString();
                    item.Flag = dt.Rows[i]["Flag"].ToString();
                    item.Log = dt.Rows[i]["Log"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public int GetCount(string Flag, int FlagLike, int UserID, string StartTime, string EndTime)
        {
            string strWhere = "";
            if (Flag != "")
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                if (FlagLike == 1)
                {
                    strWhere = strWhere + "Flag like '%" + Utils.SqlStringFormat(Flag, 2) + "%'";
                }
                else
                {
                    strWhere = strWhere + "Flag ='" + Utils.SqlStringFormat(Flag) + "'";
                }
            }
            if (UserID > 0)
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                object obj2 = strWhere;
                strWhere = string.Concat(new object[] { obj2, "UserID =", UserID, "" });
            }
            if ((StartTime != "") & Utils.IsDateTime(StartTime))
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere = strWhere + "CreateTime >='" + Utils.SqlStringFormat(StartTime) + "'";
            }
            if ((EndTime != "") & Utils.IsDateTime(EndTime))
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere = strWhere + "CreateTime <='" + Utils.SqlStringFormat(EndTime) + "'";
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("AdminLog", strWhere);
        }

        public List<KeRui.Model.AdminLog> GetList(int PageSize, int PageIndex, string Flag, int FlagLike, int UserID, string StartTime, string EndTime)
        {
            string strWhere = "";
            if (Flag != "")
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                if (FlagLike == 1)
                {
                    strWhere = strWhere + "Flag like '%" + Utils.SqlStringFormat(Flag, 2) + "%'";
                }
                else
                {
                    strWhere = strWhere + "Flag ='" + Utils.SqlStringFormat(Flag) + "'";
                }
            }
            if (UserID > 0)
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                object obj2 = strWhere;
                strWhere = string.Concat(new object[] { obj2, "UserID =", UserID, "" });
            }
            if ((StartTime != "") & Utils.IsDateTime(StartTime))
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere = strWhere + "CreateTime >='" + Utils.SqlStringFormat(StartTime) + "'";
            }
            if ((EndTime != "") & Utils.IsDateTime(EndTime))
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere = strWhere + "CreateTime <='" + Utils.SqlStringFormat(EndTime) + "'";
            }
            string strOrder = "ID desc";
            DataSet set = new KeRui.DAL.Common().GetList("AdminLog", PageSize, PageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.AdminLog> GetListByCache(int PageSize, int PageIndex, string Flag, int FlagLike, int UserID, string StartTime, string EndTime)
        {
            string cacheKey = string.Concat(new object[] { "AdminLogList-", PageSize, "-", PageIndex, "-", Flag, "-", FlagLike, "-", UserID, "-", StartTime, "-", EndTime });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(PageSize, PageIndex, Flag, FlagLike, UserID, StartTime, EndTime);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.AdminLog>) cache;
        }

        public KeRui.Model.AdminLog GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.AdminLog GetModelByCache(int ID)
        {
            string cacheKey = "AdminLogModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.AdminLog) cache;
        }

        public static DataSet GetUser()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("AdminLog", "", "", "distinct userid,username");
        }
    }
}

